<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Resources extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $service;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->service = new \app\api\service\Resources();
    }

    public function add()
    {
        $type = $this->request->param('type');
        if ($type == 'image') {
            $content = request()->file('img');
        } else {
            $content = $this->request->param('content');
        }
        try {
            $this->service->add($type, $content);
        } catch (\Throwable $exception) {
            $this->error($exception->getMessage());
        }
        $this->success('success', null, 200);
    }

    public function get()
    {
        $input = $this->request->post();
        $data = $this->service->get($input['type'], $input['search'] ?? [], $input['page'], $input['pageSize'] ?? 20);
        $this->success('success', $data, 200);
    }

    public function del()
    {
        $input = $this->request->post();
        $this->service->del($input['type'], $input['ids']);
        $this->success('success', null, 200);
    }
}
